/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2019/1/19
 * Time: 23:41
 */
$(function () {

    $(".input-group.date").datepicker({
        todayBtn: true,
        todayHighlight: true,
        keyboardNavigation: false,
        forceParse: false,
        calendarWeeks: true,
        autoclose: true,
        format: "yyyy-mm-dd",
        language: "cn",
    });

    var ue = UE.getEditor('editor', {initialFrameHeight: 250});

    sendAjax('/v1/manager/contest/detail.json', 'get', {id: $('.contest-id').val()}, function (json) {
        var data = json.body;
        $('.contest-title').val(data.title);
        $('.contest-realname').val(data.realname);
        $('.contest-create-time').val(data.created_at);
        $('.contest-type').val(data.type);
        $('.contest-type option').each(function () {
            if ($(this).val() == data.type) {
                $(this).attr('selected', 'selected');
            }
        });
         $('.contest-content').val(data.content);
         ue.setContent(data.content);
    });

    $('.btn-save').on('click', function(){
        $('.contest-content').val(ue.getContent());
        var data = {
            id: $('.contest-id').val(),
            title: $('.contest-title').val(),
            realname: $('.contest-realname').val(),
            created_at: $('.contest-create-time').val(),
            type: $('.contest-type').val(),
            content: $('.contest-content').val()
        };

        if (checkForm(data)) {
            saveData('/v1/manager/save/contest', {params:data}, '/manager/contest.html');
        }
    });
});

function checkForm(data) {
    if (data.title == '') {
        alertTip('标题不能为空');
        return false;
    }
    if (data.realname == '') {
        alertTip('姓名不能为空');
        return false;
    }
    if (data.created_at == '') {
        alertTip('发布时间不能为空');
        return false;
    }
    if (data.type == '') {
        alertTip('请选择类型');
        return false;
    }
    if (data.content == '') {
        alertTip('内容不能为空');
        return false;
    }
    return true;
}